<template>
    
</template>

<script>
    import {Toast} from "vant";
    import {loginByWX} from '@/api/api';
    import {setLocalStorage, getLocalStorage, removeLocalStorage} from '@/utils/local-storage';

    export default {
        name: "phoneToCode",
        data() {
            return {
                wxcode:'',
                userInfo: {},
                mobile:''
            }
        },
        created() {
            // let wechatCode = this.$route.params.code
            let test = window.location.search;
            let str = test.split("?code=")[1];
            let code = str.split("&")[0];
            this.wxcode = code

            console.log(str,'str')
            console.log(str.split("state=")[1],'123456')

            this.mobile = str.split("state=")[1]
            this.wxlogin()
        },
        methods: {
            wxlogin(){
                const {pid} = getLocalStorage('pid');
                var param = {
                    code: this.wxcode,
                    mobile : this.mobile
                };
                if (pid) {
                    param.parentId = pid;
                }

                loginByWX(param).then(res => {
                    console.log(res,'ressssss')
                    this.userInfo = res.data.data.userInfo
                    // if(this.userInfo.mobile){
                    //     setLocalStorage({
                    //         mobile: this.userInfo.mobile
                    //     });
                    // }
                    // setLocalStorage({
                    //     Authorization: res.data.data.token,
                    //     avatar: this.userInfo.avatarUrl,
                    //     nickName: this.userInfo.nickName,
                    //     userId: this.userInfo.userId
                    // });
                    // // 用完即删
                    // if (mobile) {
                    //     removeLocalStorage('mobile')
                    // }
                    if (pid) {
                        removeLocalStorage('pid')
                    }
                    window.location = '#/user/';
                }).catch(error => {
                    Toast.fail(error.data.errmsg);
                })
            }
        }
    }
</script>

<style scoped>

</style>